<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class WeiboController extends Controller {

    public function login() {
        if ($this->isPost()) {
            $weibo = new Weibo($this->post('username'), $this->post('password'));
            if (!empty($_GET['act'])) {
                $act = $_GET['act'];
                $weibo->$act();
            } else {
                $weibo->pre_login();
            }
        } else {
            $this->display('weibo/login');
        }
    }

    public function getWeiboContents() {
        
    }

    /**
     * 转发微博
     */
    public function forward() {
        if ($this->isPost()) {
            //内容一行代表一条
            $params['contents'] = explode("\r\n", $this->post('contents'));
            //哪些账号需要做转发操作，可选
            $params['account_ids'] = $this->post('account_ids');
            $params['is_quick'] = $this->post('is_quick');
            $params['rate'] = $this->post('rate');
            //转发哪条微博
            // http://m.weibo.cn/3044746573/EaEPBDDKW=>4025402399447618
            $data['mid'] = $this->getWeiboItemId($this->post('weibo_link'));
            $data['pdetail'] = '100505'.$this->getWeiboItemDetail($this->post('weibo_link'));
            // 一些可定制参数，是否评论至原微博，是否带评论
            $data['is_comment'] = intval($this->post('is_comment')) >= 1 ? 1 : 0;
            $data['is_comment_base'] = intval($this->post('is_comment_base')) >= 1 ? 1 : 0;
            $handler = new WeiboHandler();
            $handler->forward($data, $params);
        }
        //已经登录的账号
        $lists = DBUtils::queryList("select * from weibo");
        $this->assign('lists', $lists);
        $this->display('weibo/forward');
    }

    private function getWeiboItemId($link) {
        $codes = explode('/', $link);
        $code = end($codes);
        $arr = array_reverse(str_split(strrev($code), 4));
        $str = '';
        foreach ($arr as $v) {
            $str.=$this->base62to10(strrev($v));
        }
        return $str;
    }

    private function getWeiboItemDetail($link) {
        $codes = explode('/', $link);
        array_pop($codes);
        return end($codes);
    }

    function base62to10($n) {
        $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $n = strval($n);
        $len = strlen($n);
        $result = 0;
        $base = 1;
        for ($i = $len - 1; $i >= 0; $i--) {
            $char = $n[$i];
            $d = strpos($index, $char);
            assert($d !== false);
            $result = bcadd($result, bcmul($d, $base));
            $base = bcmul($base, 62);
        }
        return $result;
    }

    public function getweibos() {
        $weibo = new Weibo();
        print_r($weibo->request_url('http://weibo.com/comment/outbox?wvr=6&pids=plc_main&ajaxpagelet=1&ajaxpagelet_v6=1&__ref=%2Fcomment%2Finbox%3Ftopnav%3D1%26wvr%3D6%26f%3D1&_t=FM_1475129599589126', array(), '', '', 0, 0, 'cookie2070033817'));
        // print_r($weibo->request_url('http://weibo.com/2070033817',array(),'','',0,0,'cookie5057764464'));
    }

    public function sendWeibo() {
        $weibo = new Weibo();
        $post = array(
            "title" => "有什么新鲜事想告诉大家",
            "location" => "page_100505_home",
            "text" => "success",
            "appkey" => '',
            "style_type" => 1,
            "pic_id" => '',
            "pdetail" => "1005055057764465",
            "rank" => 1,
            "rankid" => '',
            "pub_source" => "page_2",
            "longtext" => 1,
            "topic_id" => '1022:',
            "pub_type" => "dialog",
            "_t" => 0
        );
        $uid = '5057764464';
        $cookie = $this->getFileCache('info' . $uid);
        $headers = array(
            'Host' => 'weibo.com',
            'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0',
            'Accept' => '*/*',
            'Accept-Language' => 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding' => 'gzip, deflate',
            'Referer' => 'http://weibo.com/u/5057764464/home?wvr=5',
            'Connection' => 'keep-alive'
        );

        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        // curl_setopt($ch, CURLOPT_REFERER, 'http://weibo.com/u/5057764464/home?wvr=5');
        // curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86          Safari/537.36');
        // curl_setopt($ch, CURLOPT_URL, 'http://weibo.com/p/aj/v6/mblog/add?ajwvr=6&domain=100505&__rnd=1475129613100');
        // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); //timeout on connect
        // curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout on response
        // //curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); //加入gzip解析
        // curl_setopt($ch, CURLOPT_POST, 1); //post方式提交 
        // curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); //要提交的信息 
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        // curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        // $output = curl_exec($ch);
        // curl_close($ch);
        // var_dump($output);die;
        print_r($weibo->request_url('http://weibo.com/p/aj/v6/mblog/add?ajwvr=6&domain=100505&__rnd=1475129613100', $post, $cookie, $headers));
    }

}
